Let's take a look at
some Pub/Sub concepts. Pub/Sub is a fully managed real-time
messaging architecture. An application that publishes messages is called a publisher. A publisher creates and
publishes messages to a topic. To receive messages, a subscriber application creates a subscription to a topic. A subscription can
use either the push or pull method for
message delivery. The subscriber only
receives messages that are published after the
subscription was created. After receiving or processing
each pending message, the subscriber sends
an acknowledgment back to the Pub/Sub service. Pub/Sub removes
acknowledged messages from the subscriptions
queue of messages. If the subscriber
doesn't acknowledge a message before the
acknowledgment deadline, Pub/Sub will resend the
message to the subscriber. Pub/Sub delivers each message to every subscription at least once. Pub/Sub enables
loosely-coupled integration between application components, acts as a buffer to handle
spikes in data volume, and also supports
other use cases. Pub/Sub supports pull
and push subscriptions. In a pull subscription, the subscriber explicitly calls the pull method to request
messages for delivery. Pub/Sub returns a message
and an acknowledgment ID. To acknowledge receipt,
the subscriber invokes the acknowledged method by
using the acknowledgment ID. This is the default
subscription model. In a pull subscription model, the subscriber could
be data flow or any application that uses Cloud Client Libraries
to retrieve messages. The subscriber controls
the rate of delivery. A subscriber can modify the acknowledgment deadline to allow more time to
process messages. To process messages rapidly, multiple subscribers can pull
from the same subscription. The pull subscription
model enables batch delivery and
acknowledgments, as well as massively
parallel consumption. Use the pull subscription
model when you need to process a very large volume of
messages with high-throughput. A push subscriber doesn't
need to implement Google Cloud library methods to retrieve and process messages. In a push subscription model, Pub/Sub sends each message
as an HTTP request to the subscriber at a
preconfigured HTTP endpoint. The push endpoint can be a load balancer or App
Engine standard application. The endpoint acknowledges
the message by returning an HTTP
success status code. A failure response indicates that the message
should be sent again. Pub/Sub dynamically
adjusts the rate of push requests based on the rate at which it
receives success responses. You can configure a default
acknowledgment deadline for push subscriptions. If your code doesn't acknowledge the message before the deadline, Pub/Sub will resend the message. Use the push
subscription model in environments where Google
Cloud dependencies, such as credentials and the
Client Library can't be configured or multiple topics must be processed by
the same web-hook. A push subscription
is also ideal when an HTTP endpoint will be invoked by Pub/Sub and
other applications. In this scenario, the push
subscribers should be able to process message payloads from
Pub/Sub and other callers. You have a choice of execution environments
for subscribers. You can deploy your subscriber
code as Cloud Functions. Your Cloud Functions will be triggered whenever a new
message is received. This method enables
you to implement a server-less approach and
build highly scalable systems. Alternatively, you can deploy your subscriber application
on a compute environment, such as Compute Engine, Google Kubernetes Engine, or App Engine
flexible environment. Multiple instances of your
application can spin up and split the workload by processing the
messages in the topic. These instances can
be automatically shut down when there are very
few messages to process. You can enable
elastic scaling using Pub/Sub metrics that are
published to Cloud Monitoring. With either approach, you
don't have to worry about developing code to manage
concurrency or scaling, where application
scales automatically depending on the workload.